package mat;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class PercCounter
{
	protected int max;
	protected int count = 0;
	protected double lastInc;
	protected double step = 0.1;
	
	protected Date dtStart = new Date();
	protected Date dtLastPerc = null;
	protected SimpleDateFormat fmtElps;
	
	public PercCounter(int max)
	{
		this.max = max;
		this.lastInc = step;
		this.fmtElps = new SimpleDateFormat("HH:mm:ss");
		this.fmtElps.setTimeZone(TimeZone.getTimeZone("GMT"));
	}
	
	public PercCounter(int max, double step)
	{
		this.max = max;
		this.step = step;
		this.lastInc = step;
		this.fmtElps = new SimpleDateFormat("HH:mm:ss");
		this.fmtElps.setTimeZone(TimeZone.getTimeZone("GMT"));
	}
	
	public int getMax()
	{
		return max;
	}
	
	public int getCount()
	{
		return count;
	}
	
	public void incCount()
	{
		count++;
	}
	
	public boolean shouldSaySomething()
	{
		double currPerc = 1.0 * count / max;
		if (currPerc >= lastInc)
		{
			dtLastPerc = new Date();
			return true;
		}
		
		return false;
	}
	
	public boolean incAndShouldSaySomething()
	{
		count++;
		
		double currPerc = 1.0 * count / max;
		if (currPerc >= lastInc)
		{
			dtLastPerc = new Date();
			return true;
		}
		
		return false;
	}
	
	public boolean incAndShouldSaySomething(int inc)
	{
		count += inc;
		
		double currPerc = 1.0 * count / max;
		if (currPerc >= lastInc)
		{
			dtLastPerc = new Date();
			return true;
		}
		
		return false;
	}
	
	protected DecimalFormat fmtPerc = new DecimalFormat("###0%"); 
	public String getSomething()
	{
		String res = count + " / " + max + " - " + fmtPerc.format(lastInc); 
		
		if (dtLastPerc != null)
		{
			long secsSoFar = Math.max((dtLastPerc.getTime() - dtStart.getTime())/1000, 1L);
			long extTotSecs = (long) (secsSoFar * 1.0 / lastInc);
						
			res += " - " + fmtElps.format(new Date(secsSoFar*1000)) + " / " + fmtElps.format(new Date(extTotSecs*1000));  
		}
		
		lastInc += step;
		return res;
	}
	
	
}


